package com.imooc.controller;

import com.imooc.common.utils.common.lang.StringUtils;
import com.imooc.pojo.BO.ShopcartBO;
import com.imooc.utils.IMOOCJSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author lihongyu
 * @CreateTime 2021-10-14 11:53
 * @Description 购物车相关接口
 */
@RestController
@RequestMapping("/shopcart")
@Api(value = "购物车相关接口",tags = "购物车相关接口")
public class ShopCarController extends BaseController {

    /**
     * 添加商品到购物车
     * @param userId 用户Id
     * @param shopcartBO 添加购物车请求封装实体
     * @param request 请求对象
     * @param response 响应对象
     * @return boolean 成功/失败
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加商品到购物车",notes = "添加商品到购物车",httpMethod = "POST")
    public IMOOCJSONResult addItemToShopcart(@RequestParam String userId,
                                             @RequestBody ShopcartBO shopcartBO,
                                             HttpServletRequest request,
                                             HttpServletResponse response){
        if (StringUtils.isBlank(userId)) {
            return IMOOCJSONResult.errorMsg("");
        }

        System.out.println(shopcartBO);

        // TODO 前端用户在登录的情况下，添加商品到购物车，会同时在后端同步购物车到redis缓存

        return IMOOCJSONResult.ok();
    }

    /**
     * 从购物车中删除物品
     * @param userId 当前登录用户ID
     * @param itemSpecId 规格Id
     * @param request 请求对象
     * @param response 响应对象
     * @return boolean 成功/失败
     */
    @ApiOperation(value = "从购物车中删除商品", notes = "从购物车中删除商品", httpMethod = "POST")
    @PostMapping("/del")
    public IMOOCJSONResult deleteItemFromShopCart(
                                                    @RequestParam String userId,
                                                    @RequestParam String itemSpecId,
                                                    HttpServletRequest request,
                                                    HttpServletResponse response) {

        if (StringUtils.isBlank(userId) || StringUtils.isBlank(itemSpecId)) {
            return IMOOCJSONResult.errorMsg("参数不能为空");
        }
        // TODO 用户在页面删除购物车中的商品数据，如果此时用户已经登录，则需要同步删除后端购物车中的商品
        return IMOOCJSONResult.ok();
    }

}
